<template>
    <van-cell-group class="card-item" v-for="(item, index) in props.list" :key="index">
        <div class="head">
            <div class="date">{{ item.date.slice(5, 7) }}月{{ item.date.slice(8) }}日</div>
            <div class="money">
                <span><i>出</i>{{
                    (item.bills.reduce((sum, bill) => {
                        if (bill.pay_type === 1) {
                            sum += parseFloat(bill.amount)
                            return sum
                        } else {
                            return sum
                        }
                    }, 0)).toFixed(2)
                }}</span>
                <span><i>入</i>{{
                    item.bills.reduce((sum, bill) => {
                        if (bill.pay_type === 2) {
                            sum += parseFloat(bill.amount)
                            return sum
                        } else {
                            return sum
                        }
                    }, 0).toFixed(2)
                }}</span>
            </div>
        </div>
        <van-cell v-for="cell in item.bills" :key="cell.id" :title=cell.type_name :value="cell.pay_type === 1 ? '-' + cell.amount : '+' +
            cell.amount" :label="`${formatteHours(new Date(Number(cell.date)))} ${cell.remark}`"
            :value-class="[cell.pay_type === 1 ? 'value-expense' : 'value-income']" @click="goDetail(cell)">
            <template #icon>
                <div class="icon" :class="[cell.pay_type === 1 ? 'icon-expense' : 'icon-income']">
                    <i class="iconfont" :class="icons[cell.type_name]"></i>
                </div>
            </template>
        </van-cell>
    </van-cell-group>
</template>

<script setup>
import formatteHours from '@/utils/hours.js'
import router from '@/router/index.js'
import icons from '@/utils/icons.js'

const props = defineProps({
    list: {
        type: Array,
        default: () => []
    }
})

const goDetail = (item) => {
    // console.log(item);
    router.push({
        path: '/detail',
        query: {
            id: item.id
        }
    })
}
</script>

<style lang="less" scoped>
.card-item {
    margin-top: 10px;
    border-radius: 10px;
    overflow: hidden;

    .head {
        height: 60px;
        display: flex;
        justify-content: space-between;
        align-items: center;
        padding: 0 15px;
        background-color: #f9f9f9;

        .date {
            font-size: 16px;
        }

        .money {
            font-size: 14px;


            span {
                margin-left: 15px;


                i {
                    display: inline-block;
                    margin-right: 5px;
                    font-weight: 200;
                    width: 20px;
                    height: 20px;
                    text-align: center;
                    line-height: 20px;
                    background-color: rgb(240, 240, 240);
                }
            }
        }
    }

    .icon {
        width: 30px;
        height: 30px;
        border-radius: 50%;
        margin: auto 0;
        margin-right: 10px;
        display: flex;
        justify-content: center;
        align-items: center;

        .iconfont {
            color: #fff;
        }
    }
}

.icon-expense {
    background-color: #37a762;
}

.icon-income {
    background-color: #F2B63B;
}

:deep(.value-expense) {
    color: #37a762;
}

:deep(.value-income) {
    color: #F2B63B;
}
</style>
